Ext.define('vacake.controller.Users', {
    extend: 'Ext.app.Controller',

    config: {
        refs: {
            mainView: 'mainView',
            usersView: 'usersView',
            moreView: 'moreView',
            usersCollect: 'usersCollect',
            usersCurrent: 'usersCurrent',
            usersFinish: 'usersFinish',
            productAccess: 'productAccess',
            usersAddress: 'usersAddress',
            accessButton: 'button[action=accessButton]',
            moreImage: 'usersView #moreImage'
        },
        control: {
            moreImage:{
                onTap:function(){
                      Ext.Viewport.mask({xtype: 'loadmask', message: '加载数据中...'  });
                      var main =this.getMainView();
                      var moreView=this.getMoreView();
                      main.setActiveItem(moreView);
                }
            },
            usersCollect: {
                itemtap: function(list, index, target, records) {
                    Ext.Viewport.mask({
                        xtype: 'loadmask',
                        message: '加载数据中...'
                    });
                    this.redirectTo('loadProduct/' + records.get('cpid'));
                }
            },
            usersCurrent: {
                itemtap: function(view, index, target, record, event) {
                    if (event.target.name == "goodsButton") {
                        Ext.Msg.confirm('是否确认收货？', '', function(data) {
                            if (data == 'yes') {
                                Ext.Viewport.mask({
                                    xtype: 'loadmask',
                                    message: '提交确认收货中...'
                                });
                                Ext.Ajax.request({
                                    url: Global.api_url + '/cloud/1/orders_update',
                                    params: {
                                        coid: event.target.dataset.coid,
                                        status: '已完成'
                                    },
                                    method: 'POST',
                                    scope: this,
                                    success: function(response) {
                                        var returnText = Ext.decode(response.responseText);
                                        cordova.exec(function(message) {}, function(error) {}, "Messages", "showMsg", [returnText.Variables.Message.messagestr]);
                                        if (returnText.Variables.Result.code == 0) {
                                            var usersCurrent = Ext.getStore('UsersCurrent');
                                            usersCurrent.load({
                                                params: {
                                                    cuid: window.localStorage.getItem('app_Login_id'),
                                                    cadid: window.localStorage.getItem('app_district_id')
                                                }
                                            });
                                        }
                                        Ext.Viewport.unmask();
                                    },
                                    failure: function() { //请求失败时执行操作
                                        Ext.Viewport.unmask();
                                        cordova.exec(function(message) {}, function(error) {}, "Messages", "showMsg", ['请求失败,服务器维护中...']);
                                    }
                                })
                            }
                        }, this);
                    }else  if (event.target.name == "payButton") {
                        Ext.Viewport.mask({ xtype: 'loadmask' });
                        var ref = window.open(Global.api_url+'/cloud/1/cloud_pay_alipay?out_trade_no='+Global.app_id+'_'+window.localStorage.getItem('app_Login_id')+'_'+event.target.dataset.ordersno, '_blank', 'location=no');
                        ref.addEventListener('loadstop', function(event) {
                            if (event.url.match("cloud_pay_index")||event.url.match("cloud_pay_back_url")) {
                                ref.close();
                                var usersCurrent = Ext.getStore('UsersCurrent');
                                usersCurrent.load({params: {cuid: window.localStorage.getItem('app_Login_id'), cadid: window.localStorage.getItem('app_district_id')}});
                                Ext.Viewport.unmask();
                            }
                        });
                        ref.addEventListener('exit', function(event) {
                            var usersCurrent = Ext.getStore('UsersCurrent');
                            usersCurrent.load({params: {cuid: window.localStorage.getItem('app_Login_id'), cadid: window.localStorage.getItem('app_district_id')}});
                            Ext.Viewport.unmask();
                        });
                    }
                }
            },
            usersFinish: {
                itemtap: function(view, index, target, record, event) {
                    if (event.target.name == "accessButton") {
                        Ext.Viewport.mask({
                            xtype: 'loadmask',
                            message: '检查中...'
                        });
                        Ext.Ajax.request({
                            url: Global.api_url + '/cloud/1/assess_product_info_find',
                            method: 'POST',
                            params: {
                                cuid: window.localStorage.getItem('app_Login_id'),
                                cpid: event.target.dataset.cpid,
                                coid: event.target.dataset.coid
                            },
                            scope: this,
                            success: function(response) {
                                Ext.Viewport.unmask();
                                var result = Ext.decode(response.responseText);
                                if (result.Variables.Result.code == 11021) {
                                    cordova.exec(function(message) {}, function(error) {}, "Messages", "showMsg", [result.Variables.Message.messagestr]);
                                    //                                    Ext.Msg.alert(result.Variables.Message.messagestr);
                                } else {
                                    var productAccess = this.getProductAccess();
                                    if (productAccess == null) {
                                        productAccess = Ext.create('vacake.view.ProductAccess');
                                    }
                                    window.localStorage.setItem("accessItem", Ext.encode({
                                        cuid: window.localStorage.getItem('app_Login_id'),
                                        cpid: event.target.dataset.cpid,
                                        coid: event.target.dataset.coid
                                    }));
                                    Ext.Viewport.animateActiveItem(productAccess, {
                                        type: 'slide',
                                        direction: 'left'
                                    });
                                }
                            },
                            failure: function() {
                                Ext.Viewport.unmask();
                                cordova.exec(function(message) {}, function(error) {}, "Messages", "showMsg", ['请求失败,服务器维护中...']);
                                //                                Ext.Msg.alert('请求失败', '服务器维护中....');
                            }
                        });
                    }
                }
            }
        }
    }

});